package org.bluedream.core.module.controller.bill.ord;

import com.baomidou.mybatisplus.core.metadata.IPage;
import lombok.extern.slf4j.Slf4j;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.bluedream.comm.base.BaseController;
import org.bluedream.comm.utils.RestResult;
import org.bluedream.core.config.advice.RsaSecurityParameter;
import org.bluedream.core.module.entity.bill.ord.Ord;
import org.bluedream.core.module.entity.bill.ord.OrdDtl;
import org.bluedream.core.module.entity.goods.Goods;
import org.bluedream.core.module.service.bill.ord.OrdDtlService;
import org.bluedream.core.module.service.bill.ord.OrdService;
import org.bluedream.core.module.service.goods.GoodsService;
import org.bluedream.core.utils.configUtil.GoalConfigUtil;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;

/**
 * @ClassName OrdController
 * @Description TODO
 * @Author foxsand
 * @Data 2021-11-19 9:02
 * @Version
 */
@Slf4j
@Controller
@RequestMapping(value = "${adminPath}/ord")
public class OrdController extends BaseController {
    @Resource
    private OrdService ordService;
    @Resource
    private OrdDtlService ordDtlService;

    @Resource
    private GoodsService goodsService;

    @ModelAttribute
    public Ord get(String id){
        return ordService.getById(id);
    }

    @RequiresPermissions("ord:view")
    @RequestMapping(value = {"/list" , ""})
    public String list(Ord entity, Model model){
        model.addAttribute("pageSize" , GoalConfigUtil.getValueByKey("sys.page.size"));
        model.addAttribute("ord" , entity);
        return "module/bill/ord/ordList";
    }

    @RequiresPermissions("ord:view")
    @RequestMapping("/form")
    public String form(Ord entity, Model model){
//        entity = ordService.getOrdBill(entity);
        model.addAttribute("ordBill" , entity);
        return "module/bill/ord/ordForm";
    }

/*    @RequiresPermissions("ord:view")
    @RequestMapping("/form4body")
    public String form4body(OrdDtl entity, Model model){
        model.addAttribute("ordDtl" , entity);
        return "module/bill/ord/ordBodyForm";
    }*/

    @RequiresPermissions("ord:edit")
    @RequestMapping("/getGoods")
    @ResponseBody
    public RestResult getGoods(Goods entity){
        return goodsService.getOne(entity);
    }

    @RequiresPermissions("ord:view")
    @RequestMapping(value = "/goodsForm")
    public String goodsForm(Model model){
        model.addAttribute("pageSize" , GoalConfigUtil.getValueByKey("sys.page.size"));
        model.addAttribute("goods" , new Goods());
        return "module/goods/goodsSelect";
    }

    @RequiresPermissions("ord:view")
    @RequestMapping("/findPage")
    @ResponseBody
    public IPage<Ord> findPage(@RsaSecurityParameter Ord entity){
        return ordService.findPage(entity);
    }

    @RequiresPermissions("ord:view")
    @RequestMapping("/findPage4Body")
    @ResponseBody
    public IPage<OrdDtl> findPage4Body(@RsaSecurityParameter Ord entity){
        return ordDtlService.findPage(entity);
    }


    @RequiresPermissions("ord:edit")
    @RequestMapping("/save")
    @ResponseBody
    public RestResult save(Ord entity){
        return ordService.saveEntity(entity);
    }

    @RequiresPermissions("ord:check")
    @RequestMapping("/check")
    @ResponseBody
    public RestResult check(Ord entity){
        return ordService.check(entity);
    }
}
